#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Agresso.Interface.Layer.Domain;
using System.Web;
using Agresso.Interface.Layer.Service;
using System.Collections;

#endregion

namespace Agresso.Interface.Layer.Web
{
    public partial class SoftmarSupplierCustomerMappingForm : Form, IProfitCentreParentForm, ISupplierMappingForm
    {
        SupplierCustomer m_suppCust;
        UserControlSoftmarSupplierCustomerMapping m_parent;
        public SoftmarSupplierCustomerMappingForm(SupplierCustomer supplierCustomer, UserControlSoftmarSupplierCustomerMapping parent)
        {
            InitializeComponent();
            this.Text = "Supplier Customer Agresso - Softmar Mapping";
            m_suppCust = supplierCustomer;
            m_parent = parent;

            textBoxCoType.Text = supplierCustomer.co_type;
            textBoxFullAddress.Text = supplierCustomer.full_address;
            textBoxShortCode.Text = supplierCustomer.short_code;
            textBoxCity.Text = supplierCustomer.city;
            textBoxShortName.Text = supplierCustomer.short_name;
            textBoxCountry.Text = supplierCustomer.country;
            textBoxFullName.Text = supplierCustomer.full_name;
            textBoxAccountCode.Text = supplierCustomer.account_code;
            textBoxActive.Text = supplierCustomer.active == "1" ? "TRUE" : "FALSE";

            textBoxAgrCustapar_id.Text = supplierCustomer.agr_cust_apar_id;
            textBoxAgrCustapar_name.Text = supplierCustomer.agr_cust_apar_name;
            textBoxAgrCustRelValue.Text = supplierCustomer.agr_cust_rel_value;
            textBoxAgrSuppapar_id.Text = supplierCustomer.agr_supp_apar_id;
            textBoxAgrSuppapar_name.Text = supplierCustomer.agr_supp_apar_name;
            //textBoxAgrSupprel_value.Text = supplierCustomer.agr_supp_profitcentre_dim_value;
            checkBoxInterco.Checked = supplierCustomer.interco;
            textBoxcusAccount.Text = supplierCustomer.cus_account;
            textBoxSupAccount.Text = supplierCustomer.sup_account;
            //textBoxLegalEntityName.Text = supplierCustomer.legal_entity_name;
            textBoxCLient.Text = supplierCustomer.client;
            textBoxCuscLient.Text = supplierCustomer.cust_client;
            textBoxAparIDInterco.Text = supplierCustomer.interco_apar_id;
            textBoxSupGroup.Text = supplierCustomer.apar_gr_id;
            textBoxCusGroup.Text = supplierCustomer.cus_apar_gr_id;

            groupBoxInterco.Enabled = checkBoxInterco.Checked;

            if (supplierCustomer.ProfitCentre.profit_centre_id > 0)
            {
                ProfitCentreService service = new ProfitCentreService();
                supplierCustomer.ProfitCentre = service.GetProfitCentreByID(supplierCustomer.ProfitCentre.profit_centre_id);
                textBoxProfitCentre.Text = supplierCustomer.ProfitCentre.agr_description;
            }
        }
        public SoftmarSupplierCustomerMappingForm(string fullName)
        {
            InitializeComponent();
            this.Text = "Supplier Customer Agresso - Softmar Mapping";
            SupplierCustomerService serviceSP = new SupplierCustomerService();
            m_suppCust = serviceSP.GetSupplierMappingByFullName(fullName);

            textBoxCoType.Text = m_suppCust.co_type;
            textBoxFullAddress.Text = m_suppCust.full_address;
            textBoxShortCode.Text = m_suppCust.short_code;
            textBoxCity.Text = m_suppCust.city;
            textBoxShortName.Text = m_suppCust.short_name;
            textBoxCountry.Text = m_suppCust.country;
            textBoxFullName.Text = m_suppCust.full_name;
            textBoxAccountCode.Text = m_suppCust.account_code;
            textBoxActive.Text = m_suppCust.active == "1" ? "TRUE" : "FALSE";

            textBoxAgrCustapar_id.Text = m_suppCust.agr_cust_apar_id;
            textBoxAgrCustapar_name.Text = m_suppCust.agr_cust_apar_name;
            textBoxAgrCustRelValue.Text = m_suppCust.agr_cust_rel_value;
            textBoxAgrSuppapar_id.Text = m_suppCust.agr_supp_apar_id;
            textBoxAgrSuppapar_name.Text = m_suppCust.agr_supp_apar_name;
            //textBoxAgrSupprel_value.Text = m_suppCust.agr_supp_profitcentre_dim_value;
            checkBoxInterco.Checked = m_suppCust.interco;
            textBoxcusAccount.Text = m_suppCust.cus_account;
            textBoxSupAccount.Text = m_suppCust.sup_account;
            //textBoxLegalEntityName.Text = m_suppCust.legal_entity_name;
            textBoxCLient.Text = m_suppCust.client;
            textBoxCuscLient.Text = m_suppCust.cust_client;
            textBoxAparIDInterco.Text = m_suppCust.interco_apar_id;
            textBoxSupGroup.Text = m_suppCust.apar_gr_id;
            textBoxCusGroup.Text = m_suppCust.cus_apar_gr_id;

            groupBoxInterco.Enabled = checkBoxInterco.Checked;

            if (m_suppCust.ProfitCentre.profit_centre_id > 0)
            {
                ProfitCentreService service = new ProfitCentreService();
                m_suppCust.ProfitCentre = service.GetProfitCentreByID(m_suppCust.ProfitCentre.profit_centre_id);
                textBoxProfitCentre.Text = m_suppCust.ProfitCentre.agr_description;
            }
        }

        public string GetCompanyName()
        {
            return textBoxShortName.Text;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateSubject();
                this.Close();
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void UpdateSubject()
        {
            m_suppCust.agr_cust_apar_id = textBoxAgrCustapar_id.Text.Trim();
            m_suppCust.agr_cust_apar_name = textBoxAgrCustapar_name.Text.Trim();
            m_suppCust.agr_cust_rel_value = textBoxAgrCustRelValue.Text.Trim();
            m_suppCust.agr_supp_apar_id = textBoxAgrSuppapar_id.Text.Trim();
            m_suppCust.agr_supp_apar_name = textBoxAgrSuppapar_name.Text.Trim();
            //m_suppCust.agr_supp_profitcentre_dim_value = textBoxAgrSupprel_value.Text;
            m_suppCust.interco = checkBoxInterco.Checked;
            m_suppCust.cus_account = textBoxcusAccount.Text.Trim();
            m_suppCust.sup_account = textBoxSupAccount.Text.Trim();
            m_suppCust.client = textBoxCLient.Text.Trim();
            m_suppCust.cust_client = textBoxCuscLient.Text.Trim();
            //m_suppCust.legal_entity_name = textBoxLegalEntityName.Text;
            m_suppCust.interco_apar_id = textBoxAparIDInterco.Text.Trim();
            m_suppCust.apar_gr_id = textBoxSupGroup.Text.Trim();
            m_suppCust.cus_apar_gr_id = textBoxCusGroup.Text.Trim();


            SupplierCustomerService service = new SupplierCustomerService();
            service.UpdateSupplierCustomerMapping(m_suppCust);
            if (m_parent != null)
                m_parent.UpdateSelectedItem(m_suppCust);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void textBoxAgrDescription_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            buttonApply.Enabled = true;
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            try
            {
                UpdateSubject();
                buttonApply.Enabled = false;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void SoftmarVesselMappingForm_Load(object sender, EventArgs e)
        {
            textBoxAgrCustapar_id.Focus();
            textBoxAgrCustapar_id.SelectAll();
        }

        private void buttonFindLegalEntity_Click(object sender, EventArgs e)
        {
            ProfitCentreListForm form = new ProfitCentreListForm(m_suppCust.ProfitCentre, this);
            form.ShowDialog();
        }

        public void UpdateProfitCentre(ProfitCentre a)
        {
            m_suppCust.ProfitCentre = a;
            textBoxProfitCentre.Text = a.agr_description;
            buttonApply.Enabled = true;
        }

        private void buttonFindCustomer_Click(object sender, EventArgs e)
        {
            FindCustomerForm form = new FindCustomerForm(this);
            form.ShowDialog();
        }

        private void buttonFindSupplier_Click(object sender, EventArgs e)
        {
            FindSupplierForm form = new FindSupplierForm(this);
            form.ShowDialog();
        }

        internal void UpdateAgressoCustomer(acuheader acu)
        {
            textBoxAgrCustapar_id.Text = acu.apar_id;
            textBoxAgrCustapar_name.Text = acu.apar_name;
            textBoxCuscLient.Text = acu.client;
            textBoxCusGroup.Text = acu.apar_gr_id;
            textBoxAgrDescription_KeyDown(null, null);
        }

        public void UpdateAgressoSupplier(asuheader asu)
        {
            textBoxAgrSuppapar_id.Text = asu.apar_id;
            textBoxAgrSuppapar_name.Text = asu.apar_name;
            textBoxCLient.Text = asu.client;
            textBoxSupGroup.Text = asu.apar_gr_id;
            textBoxAgrDescription_KeyDown(null, null);
        }

        private void checkBoxInterco_CheckedChanged(object sender, EventArgs e)
        {
            groupBoxInterco.Enabled = checkBoxInterco.Checked;
        }
    }
}